﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Security;
using System.Configuration;

using MP.Core.Authentication;
using MP.Core.Config;
using MP.Core.Library;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

/// <summary>
/// Summary description for MemberService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class MemberService : System.Web.Services.WebService
{

    public MemberService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public string ResetPassword(string userName)
    {
        string passwordNew = "";
        if (!string.IsNullOrEmpty(Util.CurrentUserName) && MPRoleProvider.CreateInstant().CheckRoles(Util.CurrentUserName, EnumModules.MEMBER, EnumActions.EDIT))
        {
            MembershipUser user = Membership.GetUser(userName);
            string password = user.ResetPassword();
            string passwordDefault = ConfigurationManager.AppSettings["PasswordDefault"];
            user.ChangePassword(password, passwordDefault);
            Membership.UpdateUser(user);
            passwordNew = passwordDefault;
        }
        return passwordNew;
    }

    [WebMethod]
    public string UpdateRoleName(string roleID, string roleName)
    {
        string _roleName = "";

        if (Util.IsGuid(roleID) && !string.IsNullOrEmpty(roleName) && MPRoleProvider.CreateInstant().CheckRoles(Util.CurrentUserName, EnumModules.MEMBER, EnumActions.EDIT))
        {
            MPRoleProvider.CreateInstant().UpdateRoleName(new Guid(roleID), roleName);
            _roleName = roleName;
        }

        return _roleName;
    }

    [WebMethod]
    public bool UpdatePosition(string BranchId, string DepartId, string JobId, string userId)
    {
        if (Membership.FindUsersByName(userId).Count <= 0)
            return false;

        HrPositionManager.CreateInstant().UpdatePosition(new Guid(BranchId), new Guid(DepartId), new Guid(JobId), userId, Util.CurrentUserName);

        return true;
    }

    [WebMethod]
    public bool UpdatePositionSub(string Id, string BranchId, string DepartId, string JobId, string userId)
    {
        if (Membership.FindUsersByName(userId).Count <= 0)
            return false;
        Guid _id = Guid.Empty;
        if (!string.IsNullOrEmpty(Id) || Util.IsGuid(Id))
            _id = new Guid(Id);

        HrPositionManager.CreateInstant().UpdatePositionSub(_id,new Guid(BranchId), new Guid(DepartId), new Guid(JobId), userId, Util.CurrentUserName);

        return true;
    }

    [WebMethod]
    public string[] UserInfo(string userName)
    {
        string[] info = new string[2];
        info[0] = userName;

        MPProfileProvider p = new MPProfileProvider();
        SettingsPropertyValueCollection s = p.GetProfileData(userName);
        if (s != null)
        {
            info[1] = s["FullName"].PropertyValue.ToString();
        }
        
        return info;
    }
    [WebMethod]
    public bool DeletePosition(Guid Id)
    {
        bool result = false;
        HrPositionEntity obj = HrPositionManager.CreateInstant().GetById(Id);
        if (obj != null)
            result = HrPositionManager.CreateInstant().Delete(obj.Id);
        return result;
    }

    [WebMethod]
    public string GetAjax()
    {
        return "get ajax success";
    }
}

